package com.zoshine.zzg.controller;

import com.github.pagehelper.PageInfo;
import com.mysql.jdbc.StringUtils;
import com.zoshine.zzg.annotation.DisableLoginCheck;
import com.zoshine.zzg.base.BaseResponse;
import com.zoshine.zzg.constant.Session;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.Goods;
import com.zoshine.zzg.pojo.GoodsType;
import com.zoshine.zzg.pojo.User;
import com.zoshine.zzg.pojo.pojostatus.CommonStatus;
import com.zoshine.zzg.pojo.pojostatus.GoodsStatus;
import com.zoshine.zzg.respons.AppResponse;
import com.zoshine.zzg.service.GoodsService;
import com.zoshine.zzg.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 商品Controller
 *
 * @author 田沛凌
 * @time 2017年6月20日 上午9:38:39
 */
@RestController
@RequestMapping(value = "goods", method = RequestMethod.POST)
public class GoodsController {
    private GoodsService goodsService;
    private GoodsTypeService goodsTypeService;

    @Autowired
    public void setGoodsTypeService(GoodsTypeService goodsTypeService) {
        this.goodsTypeService = goodsTypeService;
    }

    @Autowired
    public void setGoodsService(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    /**
     * 根据商品编号获取商品信息
     *
     * @param id 商品编号
     * @return 商品信息
     */
    @RequestMapping("/getGoods")
    public BaseResponse getGoods(Long id) {
        if (id == null){
            throw new AppException(ErrorMessage.GOODS_ID_CAN_NOT_BE_NULL_OR_UNDER_ZERO);
        }
        Goods goods = goodsService.get(id);
        return new AppResponse(goods);
    }

    /**
     * 新增商品
     *
     * @param goods
     */
    @RequestMapping("/addGoods")
    public BaseResponse addGoods(Goods goods, HttpSession session) {
        User user=(User)session.getAttribute(Session.LOGIN_USER);
        goods.setOrgId(user.getOrgId());
        goodsService.add(goods);
        return AppResponse.OK;
    }

    /**
     * 商品删除
     *
     * @param id
     */
    @RequestMapping("/delGoods")
    public BaseResponse delGoods(Long id) {
        goodsService.del(id);
        return AppResponse.OK;
    }

    /**
     * 商品批量删除
     */
    @RequestMapping("/batchDelGoods")
    public BaseResponse batchDelGoods(String delIdStr) {
        goodsService.del(delIdStr);
        return AppResponse.OK;
    }

    /**
     * 修改商品
     *
     * @param goods
     */
    @RequestMapping("/editGoods")
    public BaseResponse editGoods(Goods goods) {
        goodsService.edit(goods);
        return AppResponse.OK;
    }

    /**
     * 商品集合查询(分页)
     *
     * @param goods
     * @return 商品集合
     */
    @RequestMapping("/pageGoods")
    @SuppressWarnings("unchecked")
    public BaseResponse pageGoods(Goods goods,HttpSession session) {
        User user=(User)session.getAttribute(Session.LOGIN_USER);
        boolean flg = user.isSuperAdmin();
        if(!flg){
            goods.setOrgId(user.getOrgId());
        }
        if(StringUtils.isEmptyOrWhitespaceOnly(goods.getGoodsName())){
            goods.setGoodsName(null);
        }
        List<Goods> goodsList = goodsService.list(goods);
        return new AppResponse(new PageInfo<>(goodsList));
    }

    /**
     * 商品集合查询(不分页)
     *
     * @param goods
     * @return 商品集合
     */
    @RequestMapping("/listGoods")
    public BaseResponse listGoods(Goods goods) {
        List<Goods> goodsList = goodsService.list(goods);
        return new AppResponse(goodsList);
    }


    @RequestMapping("/pageGoodsType")
    public BaseResponse pageGoodsType() {
        List<GoodsType> goodsTypeList = goodsTypeService.list(null);
        return new AppResponse(new PageInfo<>(goodsTypeList));
    }

    /**
     * 获取所有商品类型集合
     *
     * @return
     */
    @RequestMapping("listGoodsType")
    public BaseResponse listGoodsType() {
        List<GoodsType> selectList = goodsTypeService.list(null);
        return new AppResponse(selectList);
    }

    /**
     * 获取所有商品类型集合（有商品的）
     * @return
     */
    @RequestMapping("listGoodsTypeHasGoods")
    public BaseResponse listGoodsTypeHasGoods() {
        List<GoodsType> selectList = goodsTypeService.listGoodsTypeHasGoods();
        return new AppResponse(selectList);
    }

    /**
     * 根据商品类型ID查询
     *
     * @param id
     * @return
     */
    @RequestMapping("/getGoodsType")
    public BaseResponse getGoodsType(Long id) {
        GoodsType goodsType = goodsTypeService.get(id);
        return new AppResponse(goodsType);
    }

    /**
     * 新增商品类型
     *
     * @param goodsType
     */
    @RequestMapping("/addGoodsType")
    public BaseResponse addGoodsType(GoodsType goodsType) {
        goodsTypeService.add(goodsType);
        return AppResponse.OK;
    }

    /**
     * 删除商品类型
     *
     * @param id (类型ID)
     * @return
     */
    @RequestMapping("/delGoodsType")
    public BaseResponse delGoodsType(Long id) {
        goodsTypeService.del(id);
        return AppResponse.OK;
    }

    /**
     * 修改商品类型
     *
     * @param goodsType
     */
    @RequestMapping("/editGoodsType")
    public BaseResponse editGoodsType(GoodsType goodsType) {
        goodsTypeService.edit(goodsType);
        return AppResponse.OK;
    }

    /**
     * 获取商品类型状态列表
     */
    @RequestMapping("/listGoodsTypeStatus")
    public BaseResponse listGoodsTypeStatus() {
        return new AppResponse(CommonStatus.getList());
    }

    /**
     * 获取商品状态列表
     */
    @RequestMapping("/listGoodsStatus")
    public BaseResponse listGoodsStatus() {
        return new AppResponse(GoodsStatus.getList());
    }

}
